﻿<UserControl x:Class="Passwords.Views.SettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:utils="clr-namespace:Passwords.Utils"
             xmlns:vm="clr-namespace:Passwords.ViewModels"
             xmlns:models="clr-namespace:Passwords.Models"
             xmlns:controls="clr-namespace:Passwords.Controls"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="533" d:DesignWidth="963">
    <UserControl.InputBindings>
        <KeyBinding Key="Enter"  Command="vm:ConfigCommands.Save" />
        <KeyBinding Key="Esc"  Command="vm:ConfigCommands.Undo" />
    </UserControl.InputBindings>
    <UserControl.Template>
        <ControlTemplate>
            <ControlTemplate.Resources>
                <models:AccentColor x:Key="accentColors"/>
                <utils:IntToStringConverter x:Key="intToStr"/>
                <ExponentialEase Exponent="5" x:Key="ease" EasingMode="EaseOut"/>

                <DataTemplate x:Key="colorItemTemplate">
                    <Border>
                        <DockPanel>
                            <Rectangle Margin="2"  DockPanel.Dock="Left" VerticalAlignment="Center" Width="16" Height="16" StrokeThickness="1" Stroke="#808080">
                                <Rectangle.Fill>
                                    <SolidColorBrush Color="{Binding Color}"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <TextBlock Margin="4,0" VerticalAlignment="Center" Text="{Binding Name}" FontWeight="Normal" FontSize="12"/>
                        </DockPanel>
                    </Border>
                </DataTemplate>

            </ControlTemplate.Resources>
            <Grid Margin="0,0,0,0">
                <Grid.Resources>
                    <Style TargetType="RadioButton" BasedOn="{StaticResource textRadioStyle}"/>
                    <Style TargetType="Button" BasedOn="{StaticResource iconButtonStyle}"/>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="{DynamicResource AppForeground}"/>
                        <Setter Property="VerticalAlignment" Value="Center"/>
                        <Setter Property="HorizontalAlignment" Value="Right"/>
                    </Style>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0" Margin="10,5,10,5" HorizontalAlignment="Stretch" >
                    <TextBlock Text="{Binding rs.SettingSettings, Source={StaticResource locale}}" FontSize="48" FontWeight="Light" VerticalAlignment="Top" Margin="0,-15,20,20" HorizontalAlignment="Left"/>
                    <StackPanel x:Name="appButtonPanel" VerticalAlignment="Top" HorizontalAlignment="Right"  Margin="0" Orientation="Horizontal">
                        <Button x:Name="logoutButton" Content="{Binding rs.SettingsLogout, Source={StaticResource locale}}" Style="{StaticResource radioLikeButtonStyle}"  Command="{x:Static vm:Commands.LogoutCommand}"/>
                    </StackPanel>
                </Grid>
                <TabControl Grid.Row="1" Background="Transparent" Foreground="{DynamicResource AppForeground}">
                    <TabItem Header="synchronizing">
                        <ScrollViewer HorizontalScrollBarVisibility="Disabled"  VerticalScrollBarVisibility="Auto">
                            <StackPanel >
                                <WrapPanel  HorizontalAlignment="Left"  MaxWidth="550" Margin="0,5">
                                    <WrapPanel.Resources>
                                        <Style TargetType="CheckBox">
                                            <Setter Property="Foreground" Value="{DynamicResource AppForeground}"/>
                                            <Setter Property="Margin" Value="8,4"/>
                                            <Setter Property="MinWidth" Value="250"/>
                                            <Setter Property="VerticalAlignment" Value="Center"/>
                                        </Style>
                                    </WrapPanel.Resources>

                                    <CheckBox  x:Name="serviceCb" Content="{Binding rs.RunServiceOnStartup, Source={StaticResource locale}}" 
                                           TextBlock.FontWeight="DemiBold"
                                           IsChecked="{Binding Settings.AutoStartService, Mode=TwoWay}" 
                                           MinWidth="210"
                                           Margin="8,4,8,15"  />
                                    <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
                                        <TextBlock  Text="{Binding rs.SettingsPort, Source={StaticResource locale}}" Margin="5,0,0,15"  VerticalAlignment="Center" HorizontalAlignment="Right" />
                                        <TextBox  Text="{Binding Settings.Port, FallbackValue=5000,  ValidatesOnExceptions=True}" Width="80" Margin="8,4,8,15" HorizontalAlignment="Left" VerticalAlignment="Top" />
                                    </StackPanel>
                                    <CheckBox Content="{Binding rs.SettingsDeletePasswordsOnServer, Source={StaticResource locale}}" IsChecked="{Binding Settings.SyncDeletePassword, Mode=TwoWay}" />
                                        <CheckBox Content="{Binding rs.SettingsUpdatePasswordsOnServer, Source={StaticResource locale}}" IsChecked="{Binding Settings.SyncUpdatePassword, Mode=TwoWay}"  />
                                        <CheckBox Grid.Row="1" Content="{Binding rs.SettingsDeleteCategoriesOnServer, Source={StaticResource locale}}" IsChecked="{Binding Settings.SyncDeleteCategory, Mode=TwoWay}" />
                                        <CheckBox Grid.Row="1" Content="{Binding rs.SettingsUpdateCategoriesOnServer, Source={StaticResource locale}}" IsChecked="{Binding Settings.SyncUpdateCategory, Mode=TwoWay}" />

                                </WrapPanel>
                                <TextBlock  Margin="8,20,8,0"  HorizontalAlignment="Left" FontStyle="Italic" TextWrapping="Wrap" 
                                            Text="{Binding rs.SettingsUrl, Source={StaticResource locale}}"
                                            Opacity="0.5"/>
                                <TextBox Margin="8,4" Text="{Binding Settings.ServiceUri, Mode=OneWay}" IsReadOnly="True"/>
                                <!--<Label Grid.Row="4" Grid.Column="1" Margin="8,0" Content="{Binding Settings.ServiceUri}"/>-->
                                <TextBlock  Margin="8,30,8,0"  HorizontalAlignment="Left"
                                   FontStyle="Italic"
                                   TextWrapping="Wrap"
                                           Text="{Binding rs.SettingsRegisterPort, Source={StaticResource locale}}"
                                   Opacity="0.5"/>
                                <StackPanel Orientation="Horizontal">
                                    <Button Margin="8" Content="{Binding rs.SettingsRegisterNow, Source={StaticResource locale}}" Command="vm:Commands.RegisterFirewall" Width="NaN" 
                                            Padding="10,3" HorizontalAlignment="Left" Background="Transparent" 
                                            BorderBrush="{DynamicResource AppForeground}" BorderThickness="2" Foreground="{DynamicResource AppForeground}" TextBlock.FontWeight="Bold"/>
                                    <TextBlock Text="{Binding rs.SettingsOr, Source={StaticResource locale}}" VerticalAlignment="Center"/>
                                    <Button Margin="8" Content="{Binding rs.SettingsUnregister, Source={StaticResource locale}}" Style="{StaticResource textButtonStyle}" Command="vm:Commands.UnregisterFirewall" VerticalAlignment="Center"/>
                                </StackPanel>
                            </StackPanel>
                        </ScrollViewer>

                    </TabItem>
                    <TabItem Header="{Binding rs.SettingsTheme, Source={StaticResource locale}}" >
                        <Grid VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"/>
                                <RowDefinition Height="auto"/>
                                <RowDefinition Height="auto"/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding rs.SettingsBackground, Source={StaticResource locale}}" Grid.Row="0"/>
                            <TextBlock Text="{Binding rs.SettingsAccentColor, Source={StaticResource locale}}" Grid.Row="1"/>
                            <ComboBox x:Name="bgCombo" Grid.Column="1" Width="200" Margin="4" HorizontalAlignment="Left" 
                                      ItemsSource="{Binding Path=BackgroundColors, Source= {StaticResource accentColors}}"
                                      ItemTemplate="{StaticResource colorItemTemplate}"
                                      SelectedIndex="{Binding Settings.BackgroundColorIndex}"  Tag="white"/>
                            <ComboBox x:Name="fgCombo" Grid.Column="1" Grid.Row="1" Width="200" Margin="4" 
                                      ItemsSource="{Binding Path=AccentColors, Source= {StaticResource accentColors}}"
                                      ItemTemplate="{StaticResource colorItemTemplate}"
                                      SelectedIndex="{Binding Settings.AccentColorIndex}"  Tag="white"/>
                        </Grid>
                    </TabItem>
                </TabControl>
                <StackPanel Grid.Row="2" x:Name="footer" Orientation="Horizontal"  HorizontalAlignment="Center" Margin="0,8,0,8">
                    <StackPanel.Resources>
                        <Style TargetType="Button" BasedOn="{StaticResource textButtonStyle}">
                            <Setter Property="FontSize" Value="14" />
                        </Style>
                    </StackPanel.Resources>
                    <Button Content="{Binding rs.SettingsSave, Source={StaticResource locale}}"    Command="vm:ConfigCommands.Save" />
                    <Separator Style="{StaticResource lineSepStyle}" />
                    <Button Content="{Binding rs.SettingsCancel, Source={StaticResource locale}}"  Command="vm:ConfigCommands.Undo" />
                    <StackPanel.RenderTransform>
                        <TranslateTransform x:Name="footerTransform"/>
                    </StackPanel.RenderTransform>

                </StackPanel>
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.Settings}">
                    <Setter Property="controls:FocusContainer.IsFocused" TargetName="serviceCb" Value="true"/>
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="footerTransform" Storyboard.TargetProperty="Y" From="80" To="0" EasingFunction="{StaticResource ease}" Duration="0:0:0.25"/>
                                <DoubleAnimation Storyboard.TargetName="footer" Storyboard.TargetProperty="Opacity" From="0" To="1" EasingFunction="{StaticResource ease}" Duration="0:0:1"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>
    </UserControl.Template>
</UserControl>
